from odoo import fields, models, api
from odoo.exceptions import UserError


class RPPackageType(models.Model):
    _name = 'rp.package.type'
    _description = '包裹类型'
    _sql_constraints = [('unique_name', 'UNIQUE(name)', '名称必须唯一')]

    name = fields.Char(string='名称', required=True, copy=False, index=True, translate=True)
    package_icon_id = fields.Many2one(string='包裹图标', comodel_name='rp.icon', copy=False)


class RPPackageStandards(models.Model):
    _name = "rp.package.standards"
    _description = '包裹标准'
    _sql_constraints = [('unique_name', 'UNIQUE(name)', '包裹标准名称必须唯一')]

    def _default_get_kg(self):
        kg = self.env['uom.uom'].search([('name', '=', 'kg')])
        return kg.id if kg else False

    def _default_get_cm(self):
        cm = self.env['uom.uom'].search([('name', '=', 'cm')])
        return cm.id if cm else False

    def _default_get_cm3(self):
        cm = self.env['uom.uom'].search([('name', '=', 'cm³')])
        return cm.id if cm else False

    name = fields.Char(string='名称', required=True, copy=False, index=True)
    package_type_id = fields.Many2one(string='包裹类型', comodel_name='rp.package.type', required=True)

    weight = fields.Float(string='重量', required=True)
    weight_uom_id = fields.Many2one(string='重量单位', comodel_name='uom.uom', required=True, domain=[('category_id', '=', 2)], default=_default_get_kg)
    weight_desc = fields.Char(string='重量描述', compute='_depends_fill_weight', store=True)

    depth = fields.Float(string='长度', required=True)
    width = fields.Float(string='宽度', required=True)
    height = fields.Float(string='高度', required=True)
    length_uom_id = fields.Many2one(string='长度单位', comodel_name='uom.uom', required=True, domain=[('category_id', '=', 4)], default=_default_get_cm)
    dimension_desc = fields.Char(string='尺寸描述', compute='_depends_fill_dimension', store=True)

    volume = fields.Float(string='体积', compute='_depends_compute_volume', store=True)
    volume_uom_id = fields.Many2one(string='体积单位', comodel_name='uom.uom', domain=[('category_id', '=', 5)], default=_default_get_cm3)
    volume_desc = fields.Char(string='体积描述', compute='_depends_fill_volume', store=True)

    use_type = fields.Selection(string='用途', selection=[('courier', '承运商'), ('shipper', '托运商'), ('all', '通用')], default='all')

    @api.depends('weight', 'weight_uom_id')
    def _depends_fill_weight(self):
        for s in self:
            weight = str(s.weight) if s.weight else ''
            uom = s.weight_uom_id.name if s.weight_uom_id else ''
            s.update({'weight_desc': "{} {}".format(weight, uom)})

    @api.depends('depth', 'width', 'height', 'length_uom_id')
    def _depends_fill_dimension(self):
        for s in self:
            depth = str(s.depth) if s.depth else ''
            width = str(s.width) if s.width else ''
            height = str(s.height) if s.height else ''
            uom = s.length_uom_id.name if s.length_uom_id else ''
            s.update({'dimension_desc': "{} / {} / {} {}".format(depth, width, height, uom)})

    @api.depends('depth', 'width', 'height')
    def _depends_compute_volume(self):
        for s in self:
            if s.depth and s.width and s.height:
                s.update({'volume': float(s.depth * s.width * s.height)})

    @api.depends('volume', 'volume_uom_id')
    def _depends_fill_volume(self):
        for s in self:
            volume = str(s.volume) if s.volume else ''
            uom = s.volume_uom_id.name if s.volume_uom_id else ''
            s.update({'volume_desc': '{} {}'.format(volume, uom)})
